
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>商品管理系统</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        #app {
            max-width: 800px;
            margin: 0 auto;
            background: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            padding: 10px;
            text-align: left;
            border: 1px solid #ddd;
        }
        th {
            background-color: #f8f8f8;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        input[type="text"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        button {
            padding: 10px 20px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        img {
            width: 50px;
            height: 50px;
            object-fit: cover;
        }
    </style>
</head>
<body>
    <div id="app">
        <h1>商品管理系统</h1>
        <table>
            <tr>
                <th>商品编号</th>
                <th>商品名</th>
                <th>商品价格</th>
                <th>商品图片</th>
                <th>商品库存</th>
                <th>操作</th>
            </tr>
            <tr v-for="(product, index) in products" :key="product.id">
                <td>{{ product.id }}</td>
                <td>{{ product.name }}</td>
                <td>{{ product.price }}</td>
                <td><img :src="product.image" alt="商品图片"></td>
                <td>{{ product.stock }}</td>
                <td>
                    <button @click="deleteProduct(index)">删除</button>
                </td>
            </tr>
        </table>
        <h2>添加商品</h2>
        商品编号：<input type="text" v-model="newProductId" /><br />
        商品名：<input type="text" v-model="newProductName" /><br />
        商品价格：<input type="text" v-model="newProductPrice" /><br />
        商品图片：<input type="text" v-model="newProductImage" /><br />
        商品库存：<input type="text" v-model="newProductStock" /><br />
        <button @click="addProduct">添加</button>
    </div>

    <script>
        const app = Vue.createApp({
            data() {
                return {
                    newProductId: '',
                    newProductName: '',
                    newProductPrice: '',
                    newProductImage: '',
                    newProductStock: '',
                    products: [
                        { id: 1, name: 'iPhone14', price: 8888, image: './img/iphone14.jpg', stock: 5 },
                        { id: 2, name: '华为Mate40', price: 8888, image: './img/mate40.jpg', stock: 3 }
                    ]
                };
            },
            methods: {
                addProduct() {
                    const newProduct = {
                        id: parseInt(this.newProductId),
                        name: this.newProductName,
                        price: parseInt(this.newProductPrice),
                        image: this.newProductImage,
                        stock: parseInt(this.newProductStock)
                    };
                    this.products.push(newProduct);
                    // 清空输入框
                    this.newProductId = '';
                    this.newProductName = '';
                    this.newProductPrice = '';
                    this.newProductImage = '';
                    this.newProductStock = '';
                },
                deleteProduct(index) {
                    this.products.splice(index, 1);
                }
            }
        });

        const vm = app.mount('#app');
    </script>
</body>
</html>
